/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.repository;

import me.zhengjie.dto.sqlServer.Pallet;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.sql.Timestamp;
import java.util.List;

/**
* @website https://eladmin.vip
* @author zhangYY
* @date 2024-01-12
**/
public interface PalletRepository extends JpaRepository<Pallet, Integer>, JpaSpecificationExecutor<Pallet> {
    /**
    * 根据 PalletCode 查询
    * @param palletCode /
    * @return /
    */
    Pallet findPalletByPalletCode(String palletCode);

    @Query("select p.palletCode from Pallet p where p.id =?1")
    String findPalletCodeById(int id);

    @Modifying
    @Query("update Pallet p set p.flowTo=?1, p.processcardId=?2, p.updateTime=?3, p.updateBy=?4, p.layUpStationCode=?5 where p.id=?6")
    void bindingProcessCard(Integer flowTo, Integer processcardId, Timestamp updateTime, String userName, String stationCode, Integer id);

    @Modifying
    @Query("update Pallet p set p.processcardId=null where p.palletCode=?2")
    void unbindingPalletAndProcessCard(String palletCode);


    @Query("select p from Pallet p where p.layUpStationCode=?1 order by p.updateTime desc")
    List<Pallet> findPalletOrOrderByUpdateTimeAndStationCode(String stationCode);

    Pallet findPalletById(Integer id);

    @Modifying
    @Query("update Pallet p set p.flowTo=?2 where p.id=?1")
    int updateFlowToByPalletId(Integer palletId, Integer flowTo);

    @Modifying
    @Query("update Pallet p set p.processcardId=?2 where p.id=?1")
    int updateProcesscardById(Integer id, Integer processcardId);
}